package com.yungu.swift.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

/**
 * @author Zerrion
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    private static final Logger logger = LoggerFactory.getLogger(DateUtils.class);

    private static final ThreadLocal<DateFormat> DEFAULT_FORMATTER = new ThreadLocal<DateFormat>() {

        @Override
        protected DateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
    };

    private DateUtils() {
    }

    public static String format(Date date) {
        return DEFAULT_FORMATTER.get().format(date);
    }

    public static String format(Date date, String pattern) {
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(date);
    }

    public static String format(Timestamp timestamp) {
        return DEFAULT_FORMATTER.get().format(timestamp);
    }

    public static String format(Timestamp timestamp, String pattern) {
        if (StringUtils.isEmpty(pattern)) {
            pattern = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat(pattern);
        return dateFormat.format(timestamp);
    }


    public static Date parse(String dateStr) {
        try {
            return DEFAULT_FORMATTER.get().parse(dateStr);
        } catch (ParseException e) {
            logger.error("DateUtils parse ParseException,dateStr:{}", dateStr, e);
        }
        return null;
    }

    public static Date parse(String dateStr, String pattern) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(pattern);
            return sdf.parse(dateStr);
        } catch (ParseException e) {
            logger.error("DateUtils parse ParseException,dateStr:{},pattern:{}", dateStr, pattern, e);
        }
        return null;
    }

    /**
     * 根据传进来的毫秒数拼接时分秒
     *
     * @param timeMillis
     * @return
     */
    public static String getTimeStr(long timeMillis) {
        int seconds = (int) timeMillis / 1000;
        StringBuffer timeStr = new StringBuffer();
        if (seconds > 3600) {
            int hour = seconds / 3600;
            timeStr.append(hour).append("小时");
            seconds = seconds - (hour * 3600);
            if (seconds > 60) {
                int minute = seconds / 60;
                timeStr.append(minute).append("分钟");
                seconds = seconds - (minute * 60);
                if (seconds > 0) {
                    timeStr.append(seconds).append("秒");
                }
            } else if (seconds > 0) {
                timeStr.append(seconds).append("秒");
            }
        } else if (seconds > 60) {
            int minute = seconds / 60;
            timeStr.append(minute).append("分钟");
            seconds = seconds - (minute * 60);
            if (seconds > 0) {
                timeStr.append(seconds).append("秒");
            }
        } else if (seconds > 0) {
            timeStr.append(seconds).append("秒");
        }
        return timeStr.toString();
    }

    public static boolean compareByDay(Date from, Date to, int days) {
        if (from == null || to == null) {
            return false;
        }
        if (to.getTime() < from.getTime()) {
            return false;
        }
        return (Math.abs(to.getTime() - from.getTime())) >= days * 24 * 60 * 60 * 1000;
    }

    public static boolean compareByMins(Date from, Date to, int mins) {
        if (from == null || to == null) {
            return false;
        }
        if (to.getTime() < from.getTime()) {
            return false;
        }
        return (to.getTime() - from.getTime()) >= mins * 60 * 1000;
    }

    /**
     * 获取今日剩余时间
     * @return
     */
    public static Long getTodayRemainingSecond(){
        LocalDateTime midnight = LocalDateTime.now().plusDays(1).withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime now = LocalDateTime.now();
        Long seconds = ChronoUnit.SECONDS.between(now, midnight);
        return seconds;
    }

    /**
     * 获取今日日期 day 例如 2020-3-5 的5
     */
    public static Integer getNowDay(){
        LocalDateTime now = LocalDateTime.now();
        return now.getDayOfMonth();
    }

}
